<template>
  <uni-nav-bar :title="title" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package" style="font-size: 26rpx;" v-if="wareInfor">
    <view style="font-size: 30rpx;">{{wareInfor.name}}（ID:{{wareInfor.id}}）</view>
    <view style="margin:5rpx 0px;">负责人：{{wareInfor.admin_name}}</view>
    <view>{{wareInfor.address}}</view>
  </view>
  <!-- 切换 -->
  <view class="package" style="padding:0px;">
    <z-tabs ref="tabs" :list="list" :current="current" @change="tabsChange" inactive-color="#333" />
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package space-between"
      @click="navTo('administrator/storehouse/shopList?shopID='+item.id+'&wareID='+wareID)"
      v-for="(item,index) in frinedList" :key="item.id" :style="{marginTop:index==0?'0px':'15rpx'}">
      <view style="width:85%;">{{item.name}}</view>
      <view>{{item.nums}}件</view>
    </view>
    <Popu v-if="frinedList.length<1" :pageHeight="pageHeight" text="暂无商品列表"></Popu>
  </scroll-view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { navBack, navTo } from '@/utils/navigator';
  import { showLoading } from '@/utils/prompt';
  import { onLoad, onShow } from '@dcloudio/uni-app';
  import Popu from '@c/earthPushing/common/popu.vue';
  import { getKuInfo, getKuGoodsCate, getKuGoodsList } from '@/gql/warehouse';
  import { toPublish } from '@mqtt';
  /**
   * 仓库信息
   * @param list 仓库商品类型信息
   * @param current 选项索引-
   * @param title 页面标题
   * @param wareInfor 当前仓库详情
   * @param wareID 仓库ID
   * @param pageHeight 页面高度
   * @param frinedList 商品列表
   * @param lastPage 总页数
   * @param currentPage 当前页数
   */
  const list = ref([])
  const current = ref(0)
  const title = ref()
  const wareID = ref()
  const wareInfor = ref()

  const pageHeight = ref();
  const frinedList = ref([])
  const lastPage = ref(1);
  const currentPage = ref(1);
  const listID = ref(0)
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 210;
    },
  });
  onLoad((pearm) => {
    wareID.value = Number(pearm.id)
    init()
    classList()
  })
  onShow(() => {
    if (wareID.value) {
      list.value = []
      current.value = 0
      currentPage.value = 1
      frinedList.value = []
      classList()
      wareList()
    }
  })
  //获取仓库详情
  function init() {
    showLoading('正在加载');
    const payload = {
      query: getKuInfo,
      variables: {
        ku_id: wareID.value,
      },
    };
    toPublish('ql/control/getKuInfo', payload, (obj : any) => {
      uni.hideLoading();
      const { getKuInfo } = obj.data;
      wareInfor.value = getKuInfo;
      title.value = getKuInfo.name
    });
  }
  //获取仓库商品分类
  function classList() {
    showLoading('正在加载');
    const payload = {
      query: getKuGoodsCate,
      variables: {
        ku_id: wareID.value,
      },
    };
    toPublish('ql/control/getKuGoodsCate', payload, (obj : any) => {
      uni.hideLoading();
      const { getKuGoodsCate } = obj.data;
      list.value = getKuGoodsCate;
      listID.value = getKuGoodsCate && getKuGoodsCate[0].id
      wareList()
    });
  }
  /**
   * 获取商品信息
   * @param {Number} page  获取商品列表数据
   * @param {String} keywords  搜索信息
   */
  function wareList() {
    showLoading()
    const payload = {
      query: getKuGoodsList,
      variables: {
        page: currentPage.value,
        cid: Number(listID.value),
        ku_id: wareID.value,
      },
    };
    toPublish(
      'ql/control/getKuGoodsList',
      payload,
      (obj : any) => {
        const { getKuGoodsList } = obj.data;
        frinedList.value = [...frinedList.value, ...getKuGoodsList.list];
        lastPage.value = getKuGoodsList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      wareList();
    }
  }
  //tabs通知swiper切换
  function tabsChange(index : number) {
    current.value = index;
    listID.value = list.value[index].id
    currentPage.value = 1
    frinedList.value = []
    wareList();
  }
</script>

<style scoped lang="less">
</style>